<template>
  <div>
    <el-form ref="form" :model="form" class="dataForm cgForm">
      <el-table v-loading="loading" :data="form.fields" border tooltip-effect="darkTable">
        <el-table-column :label="$t('分组标题')" align="center" prop="groupTitle">
          <template slot-scope="scope">
            <el-form-item :prop="'fields.'+scope.$index + '.groupTitle'" :rules="rules.groupTitle">
              <el-input v-model="scope.row.groupTitle" :placeholder="$t('分组标题')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('字段')" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'fields.'+scope.$index + '.dbFieldNameList'" :rules="rules.dbFieldNameList">
              <el-select v-model="scope.row.dbFieldNameList" :placeholder="$t('请选择字段')" multiple>
                <el-option v-for="field in fields" :key="field.dbFieldName" :label="$t(field.dbFieldTxt)" :value="field.dbFieldName" />
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('显示条件')" min-width="180" align="center">
          <template slot-scope="scope">
            <el-row>
              <el-col :span="8">
                <el-form-item :prop="'fields.'+scope.$index + '.showField'">
                  <el-input v-model="scope.row.showField" :placeholder="$t('字段名')" />
                </el-form-item>
              </el-col>
              <el-col :span="7" style="margin: 0 5px;">
                <el-form-item :prop="'fields.'+scope.$index + '.showCondition'">
                  <el-select v-model="scope.row.showCondition" :placeholder="$t('选择条件')">
                    <el-option v-for="item in conditions" :key="item.value" :value="item.value" :label="$t(item.label)" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item :prop="'fields.'+scope.$index + '.showValue'">
                  <el-input v-model="scope.row.showValue" :placeholder="$t('值')" />
                </el-form-item>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="120">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.$index)"
            >{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        style="margin-top: 20px;"
        @click="addHandle"
      >{{ $t('新增') }}</el-button>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <el-button @click="submit">确认</el-button>
    </div>
  </div>
</template>

<script>
import {
  exportOnlCgformGroupField,
  listOnlCgformGroupField
} from '@/api/system/desform/onlineForm/onlCgformGroupField'
export default {
  name: 'OnlCgformGroupField',
  props: {
    headId: {
      type: String,
      default: undefined
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: '分组字段',
      fields: [],
      // 表单参数
      form: {
        fields: []
      },
      // 表单校验
      rules: {
        dbFieldNameList: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ],
        groupTitle: [
          { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
        ]
      },
      conditions: [
        {
          label: '等于',
          value: '=='
        },
        {
          label: '大于',
          value: '>'
        },
        {
          label: '小于',
          value: '<'
        },
        {
          label: '大于等于',
          value: '>='
        },
        {
          label: '小于等于',
          value: '<='
        },
        {
          label: '不等于',
          value: '!='
        }
      ]
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    initGroupField(fields) {
      this.loading = true
      this.fields = fields
      this.reset()
      this.getList()
      this.loading = false
    },
    getList() {
      if (this.headId) {
        this.loading = true
        listOnlCgformGroupField({ headId: this.headId }).then(res => {
          this.form.fields = res.rows
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        fields: []
      }
      this.resetForm('form')
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$emit('group', this.form.fields)
          this.cancelForm()
        }
      })
    },
    cancelForm() {
      if (this.$parent.$parent.groupOpen) {
        this.$parent.$parent.groupOpen = false
      }
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
    },
    /** 新增按钮操作 */
    addHandle() {
      var field = {
        id: undefined,
        dbFieldNameList: [],
        dbFieldTxt: undefined,
        groupTitle: !this.form.fields || this.form.fields.length === 0 ? undefined : this.form.fields[this.form.fields.length - 1].groupTitle,
        fieldOrder: this.form.fields.length + 1,
        showField: '',
        showCondition: '',
        showValue: ''
      }
      this.form.fields.push(field)
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.onlCgformGroupFieldForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(index) {
      // this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
      //   confirmButtonText: this.$t('确定'),
      //   cancelButtonText: this.$t('取消'),
      //   type: 'warning'
      // }).then(() => {
      //   return delOnlCgformGroupField(row.id)
      // }).then(() => {
      //   this.getList()
      //   this.msgSuccess(this.$t('删除成功'))
      // }).catch(() => {
      //   this.loading = false
      // })
      this.form.fields.splice(index, 1)
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return exportOnlCgformGroupField(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped lang="scss">
/deep/.cgForm .is-error{
  height: 55px;
}
</style>
